import { ref, watch, computed } from 'vue';
/**
 * 本地分页hooks
 * @returns
 */
export default function ({ data }) {
  /**
   * 分页相关
   */
  const pagination = ref({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    onCurrentChange(page) {
      pagination.value.currentPage = page;
    },
    onSizeChange(pageSize) {
      pagination.value.pageSize = pageSize;
    },
  });

  watch(data, () => {
    pagination.value.total = data.value.length;
    pagination.value.currentPage = 1;
  });

  /**
   * 当前页的数据
   */
  const currentPageData = computed(() => {
    if (!data.value?.length) return [];
    const start = (pagination.value.currentPage - 1) * pagination.value.pageSize;
    const allData = data.value;
    return allData.slice(start, start + pagination.value.pageSize);
  });

  return {
    data: currentPageData,
    pagination,
  };
}
